using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;
using System.Collections.Generic;

namespace Nate.GUI.Canvas.Component
{
    public class MouseMove : IComponent
    {
        public MouseMove(Base canvas)
        {
            _canvas             = canvas;
            _refresh_location   = Point.Empty;
            _last_mouse_pos     = Point.Empty;
            _start_mouse_pos    = Point.Empty;
        }

        public void InitializeComponent()
        {
            _canvas.MouseMove += new MouseEventHandler(_MouseMove);
            _canvas.MouseDown += new MouseEventHandler(_MouseDown);
            _canvas.MouseUp += new MouseEventHandler(_MouseUp);
        }

        public void Tick()
        {
            if (!_refresh_location.IsEmpty)
            {
                _canvas.Location = _refresh_location;
                _refresh_location = Point.Empty;
            }
        }

        private void _MouseDown(object sender, MouseEventArgs e)
        {
            if (MouseButtons.Middle == e.Button)
            {
                Cursor.Hide();
            }
        }

        private void _MouseUp(object sender, MouseEventArgs e)
        {
            if (MouseButtons.Middle == e.Button)
            {
                Cursor.Show();
                Cursor.Position = _canvas.PointToScreen(_start_mouse_pos);
            }
        }

        private void _MouseMove(object sender, MouseEventArgs e)
        {
            if (MouseButtons.Middle == e.Button)
            {
                if (_last_mouse_pos.IsEmpty)
                {
                    _last_mouse_pos = _start_mouse_pos = e.Location;
                    return;
                }

                Point current = !_refresh_location.IsEmpty ? _refresh_location : _canvas.Location;
                _refresh_location = new Point();
                _refresh_location.X = _canvas.Location.X - (_last_mouse_pos.X - e.Location.X);
                _refresh_location.Y = _canvas.Location.Y - (_last_mouse_pos.Y - e.Location.Y);

                _last_mouse_pos = e.Location;
                _canvas.RefreshCanvas();
                return;
            }

            _last_mouse_pos = _start_mouse_pos = Point.Empty;
        }

        private Base _canvas;
        private Point _refresh_location;
        private Point _last_mouse_pos;
        private Point _start_mouse_pos;
    }
}